package com.share.example.spider.schedule;

import com.share.example.spider.model.db.Stock;
import com.share.example.spider.pipeline.StockDayPipeline;
import com.share.example.spider.processor.StockDayProcessor;
import com.share.example.spider.service.IStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import javax.annotation.PostConstruct;
import java.util.List;

@Slf4j
@Component
public class StockDaySchedule {

    @Autowired
    private IStockService iStockService;

    @Autowired
    private StockDayPipeline stockpipeline;

    @PostConstruct
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void pullStock() {
        log.info("开始异步提交任务");
        Spider spider = Spider.create(new StockDayProcessor());/*这里只能用new*/
        //股票列表
        List<Stock> tmpList = iStockService.list();
        for (Stock sk : tmpList) {
            String sss = converCode(sk.getCode().trim());
            String url = String.format("http://quote.eastmoney.com/%s.html", sss);
            spider.addUrl(url);
        }

        spider.addPipeline(stockpipeline);
        spider.thread(5);
        spider.setExitWhenComplete(true);
        spider.start();
        log.info("异步任务提交结束!");
    }

    private String converCode(String code) {
        if (code == null) {
            return code;
        } else if (code.startsWith("00")) {
            return "sz" + code;
        } else if (code.startsWith("00")) {
            return "sz" + code;
        } else {
            return code;
        }
    }
}


